<template>
  <header class="main-header" :class="{ scrolled: isScrolled }">
    <div class="header-container">
      <!-- 左侧品牌Logo -->
      <div class="header-brand">
        <router-link to="/" class="brand-link" @click="setActiveNav('home')">
          <div class="brand-logo">
            <span class="logo-icon">🍷</span>
            <span class="logo-text">HUAWEI</span>
          </div>
        </router-link>
      </div>

      <!-- 中间主导航 -->
      <nav class="main-nav">
        <div class="nav-links">
          <router-link 
            v-for="item in navItems" 
            :key="item.id"
            :to="item.path" 
            class="nav-link"
            :class="{ active: activeNav === item.id }"
            @click="setActiveNav(item.id)"
            :ref="setNavLinkRef"
          >
            <span class="link-text">{{ item.text }}</span>
            <!-- 单独的下划线，每个导航项都有自己的 -->
            <div class="link-underline" :class="{ active: activeNav === item.id }"></div>
          </router-link>
        </div>
        
        <!-- 或者使用这个移动的下划线 -->
        <div class="nav-indicator" :style="indicatorStyle" v-if="showIndicator"></div>
      </nav>

      <!-- 右侧功能区域 -->
      <div class="header-actions">
        <!-- 搜索框 -->
        <div class="search-container">
          <button class="search-btn" @click="toggleSearch">
            <span class="search-icon">🔍</span>
          </button>
          <div class="search-box" :class="{ expanded: isSearchExpanded }">
            <input 
              type="text" 
              placeholder="搜索产品、技术..." 
              class="search-input"
              v-model="searchQuery"
            />
          </div>
        </div>

        <!-- 用户登录 -->
        <div class="user-container">
          <button class="user-btn" @click="toggleUserMenu">
            <span class="user-icon">👤</span>
            <span class="user-text" v-if="!user">登录</span>
            <span class="user-name" v-else>用户</span>
          </button>
        </div>

        <!-- 购物车 -->
        <div class="cart-container">
          <router-link to="/cart" class="cart-btn">
            <span class="cart-icon">🛒</span>
            <span class="cart-count" v-if="cartCount > 0">{{ cartCount }}</span>
          </router-link>
        </div>
      </div>
    </div>
  </header>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted, nextTick } from 'vue'
import { useRoute } from 'vue-router'

const route = useRoute()

// 响应式数据
const isScrolled = ref(false)
const activeNav = ref('home')
const isSearchExpanded = ref(false)
const searchQuery = ref('')
const user = ref(null)
const cartCount = ref(3)
const navLinkRefs = ref([])
const showIndicator = ref(false)

// 导航项配置
const navItems = ref([
  { id: 'home', text: '首页', path: '/' },
  { id: 'products', text: '产品中心', path: '/products' },
  { id: 'technology', text: '技术创新', path: '/technology' },
  { id: 'about', text: '品牌故事', path: '/about' },
  { id: 'support', text: '服务支持', path: '/support' }
])

// 设置导航链接引用
const setNavLinkRef = (el) => {
  if (el) {
    navLinkRefs.value.push(el)
  }
}

// 计算下划线位置和宽度 - 简化版本
const indicatorStyle = computed(() => {
  const activeIndex = navItems.value.findIndex(item => item.id === activeNav.value)
  
  if (activeIndex === -1 || !navLinkRefs.value[activeIndex]) {
    return { opacity: 0 }
  }

  const activeLink = navLinkRefs.value[activeIndex]
  const linkRect = activeLink.getBoundingClientRect()
  
  // 简单的百分比计算
  const position = activeIndex * 20 // 每个占20%
  const width = 20 // 每个宽度20%
  
  console.log('下划线计算:', { activeIndex, position, width }) // 调试信息
  
  return {
    transform: `translateX(${position}%)`,
    width: `${width}%`,
    opacity: 1
  }
})

// 设置当前激活的导航
const setActiveNav = (nav) => {
  activeNav.value = nav
  isSearchExpanded.value = false
  console.log('激活导航:', nav) // 调试信息
}

// 切换搜索框
const toggleSearch = () => {
  isSearchExpanded.value = !isSearchExpanded.value
}

// 切换用户菜单
const toggleUserMenu = () => {
  console.log('打开用户菜单')
}

// 处理滚动事件
const handleScroll = () => {
  isScrolled.value = window.scrollY > 50
}

// 根据路由设置激活状态
const setActiveFromRoute = () => {
  const routeMap = {
    '/': 'home',
    '/products': 'products',
    '/technology': 'technology',
    '/about': 'about',
    '/support': 'support'
  }
  activeNav.value = routeMap[route.path] || 'home'
}

// 初始化下划线
const initIndicator = () => {
  nextTick(() => {
    showIndicator.value = true
    console.log('下划线初始化完成') // 调试信息
  })
}

// 生命周期
onMounted(() => {
  window.addEventListener('scroll', handleScroll)
  setActiveFromRoute()
  initIndicator()
  
  // 确保DOM更新后重新计算
  nextTick(() => {
    activeNav.value = activeNav.value
  })
})

onUnmounted(() => {
  window.removeEventListener('scroll', handleScroll)
})
</script>

<style scoped>
.main-header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  background: rgba(20, 20, 20, 0.95);
  backdrop-filter: blur(20px);
  border-bottom: 1px solid rgba(179, 0, 0, 0.3);
  box-shadow: 0 4px 25px rgba(0, 0, 0, 0.6);
  transition: all 0.3s ease;
  z-index: 1000;
}

.main-header.scrolled {
  background: rgba(20, 20, 20, 0.98);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.8);
}

.header-container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 40px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 70px;
}

/* 品牌Logo样式 */
.header-brand {
  flex: 0 0 auto;
}

.brand-link {
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 12px;
  transition: all 0.3s ease;
}

.brand-link:hover {
  transform: scale(1.05);
}

.brand-logo {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo-icon {
  font-size: 1.8rem;
  filter: drop-shadow(0 0 10px rgba(179, 0, 0, 0.5));
}

.logo-text {
  font-size: 1.5rem;
  font-weight: 700;
  background: linear-gradient(135deg, #ffffff, #b30000);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: 1px;
}

/* 主导航样式 */
.main-nav {
  flex: 1;
  display: flex;
  justify-content: center;
  position: relative;
  max-width: 600px;
  height: 100%;
}

.nav-links {
  display: flex;
  gap: 0;
  width: 100%;
  position: relative;
  height: 100%;
}

.nav-link {
  flex: 1;
  text-decoration: none;
  color: #cccccc;
  font-weight: 500;
  font-size: 1rem;
  padding: 0 20px;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  position: relative;
  background: transparent;
  border: none;
  cursor: pointer;
  white-space: nowrap;
}

.nav-link:hover {
  color: #ffffff;
  background: rgba(179, 0, 0, 0.1);
}

.nav-link.active {
  color: #ffffff;
  font-weight: 600;
}

.link-text {
  position: relative;
  z-index: 2;
}

/* 每个导航项自带的静态下划线 */
.link-underline {
  position: absolute;
  bottom: 0;
  left: 20%;
  right: 20%;
  height: 0;
  background: #b30000;
  transition: all 0.3s ease;
  opacity: 0;
}

.link-underline.active {
  height: 3px;
  opacity: 1;
}

.link-underline.active::before {
  content: '';
  position: absolute;
  top: 1px;
  left: 0;
  right: 0;
  height: 1px;
  background: rgba(255, 255, 255, 0.6);
}

/* 移动的下划线指示器 */
.nav-indicator {
  position: absolute;
  bottom: 0;
  left: 0;
  height: 3px;
  width: 20%;
  background: linear-gradient(90deg, 
    transparent 0%, 
    #b30000 20%, 
    #b30000 80%, 
    transparent 100%
  );
  transition: all 0.4s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  opacity: 1; /* 强制显示 */
  pointer-events: none;
  z-index: 10;
  border: 2px solid red; /* 调试边框 */
}

.nav-indicator::before {
  content: '';
  position: absolute;
  top: 1px;
  left: 10%;
  right: 10%;
  height: 1px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 1px;
}

.nav-indicator::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 20%;
  right: 20%;
  height: 1px;
  background: rgba(255, 255, 255, 0.5);
  border-radius: 1px;
}

/* 右侧功能区域 */
.header-actions {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 20px;
}

/* 搜索框样式 */
.search-container {
  position: relative;
  display: flex;
  align-items: center;
}

.search-btn {
  background: transparent;
  border: none;
  color: #cccccc;
  font-size: 1.2rem;
  cursor: pointer;
  padding: 8px;
  border-radius: 8px;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.search-btn:hover {
  color: #ffffff;
  background: rgba(179, 0, 0, 0.1);
}

.search-box {
  position: absolute;
  right: 100%;
  top: 50%;
  transform: translateY(-50%);
  width: 0;
  opacity: 0;
  transition: all 0.3s ease;
  overflow: hidden;
}

.search-box.expanded {
  width: 250px;
  opacity: 1;
  margin-right: 10px;
}

.search-input {
  width: 100%;
  padding: 10px 15px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 20px;
  color: #ffffff;
  font-size: 0.9rem;
  backdrop-filter: blur(10px);
}

.search-input::placeholder {
  color: #999999;
}

.search-input:focus {
  outline: none;
  border-color: rgba(179, 0, 0, 0.5);
  background: rgba(255, 255, 255, 0.15);
}

/* 用户按钮样式 */
.user-container {
  position: relative;
}

.user-btn {
  background: transparent;
  border: none;
  color: #cccccc;
  font-size: 1rem;
  cursor: pointer;
  padding: 8px 15px;
  border-radius: 20px;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 8px;
}

.user-btn:hover {
  color: #ffffff;
  background: rgba(179, 0, 0, 0.1);
}

.user-icon {
  font-size: 1.1rem;
}

/* 购物车样式 */
.cart-container {
  position: relative;
}

.cart-btn {
  position: relative;
  background: transparent;
  border: none;
  color: #cccccc;
  font-size: 1.2rem;
  cursor: pointer;
  padding: 8px;
  border-radius: 8px;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
}

.cart-btn:hover {
  color: #ffffff;
  background: rgba(179, 0, 0, 0.1);
}

.cart-count {
  position: absolute;
  top: -5px;
  right: -5px;
  background: linear-gradient(135deg, #b30000, #ff4757);
  color: white;
  font-size: 0.7rem;
  font-weight: 600;
  min-width: 18px;
  height: 18px;
  border-radius: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .header-container {
    padding: 0 20px;
  }
  
  .nav-link {
    padding: 0 15px;
    font-size: 0.9rem;
  }
  
  .search-box.expanded {
    width: 200px;
  }
}

@media (max-width: 768px) {
  .header-container {
    padding: 0 15px;
  }
  
  .logo-text {
    display: none;
  }
  
  .nav-link {
    padding: 0 10px;
    font-size: 0.85rem;
  }
  
  .user-text, .user-name {
    display: none;
  }
  
  .search-box.expanded {
    width: 180px;
  }
}
</style>